﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yuans.BinaryConfig;
using Yuans.BinaryConfig.OperationBoardControl;

namespace BinaryConfigEditor
{
    public partial class MainForm : Form
    {
        ConfigManager configManager;
        ConfigItem currentConfigItem;

        BoolItemOperationBoard boolItemOperationBoard = new BoolItemOperationBoard();
        BlobItemOperationBoard blobItemOperationBoard = new BlobItemOperationBoard();
        NumberItemOperationBoard numberItemOperationBoard = new NumberItemOperationBoard();
        StringItemOperationBoard stringItemOperationBoard = new StringItemOperationBoard();
        DateTimeItemOperationBoard dateTimeOperationBoard = new DateTimeItemOperationBoard();

        string currentConfigFilePath;

        private void InitialMatters()
        {
            InitializeComponent();

            configManager = new ConfigManager(new List<ConfigItem>());
            currentConfigItem = new ConfigItem();

            configManager.GenTestItems();

            keyNameTextBox.Text = "";
            keyTypeSelComboBox.Items.AddRange(GetItemTypeComboBoxEnumString());
            keyTypeSelComboBox.SelectedIndex = 0;

            ConfigItemListViewInit(configManager.ConfigList);
        }
        public MainForm()
        {
            InitialMatters();
        }
        public MainForm(string[] args)
        {
            InitialMatters();
            configManager.LoadConfigFromFile(args[0]);
            ConfigItemListViewInit(configManager.ConfigList);         
        }

        private string[] GetItemTypeComboBoxEnumString()
        {
            List<string> enumStringList = new List<string>();
            foreach (string str in Enum.GetNames(typeof(ConfigItem.ParamType)))
            {
                enumStringList.Add(str);
            }
            return enumStringList.ToArray();
        }

        private ConfigItem ControlsToConfigItem()
        {
            ConfigItem configItem = null;
            ConfigItem.ParamType itemType = (ConfigItem.ParamType)keyTypeSelComboBox.SelectedIndex;
            string itemName = keyNameTextBox.Text;
            switch (itemType)
            {
                case ConfigItem.ParamType.BLOB:
                    {
                        configItem = new BlobItem(itemName, blobItemOperationBoard.ImportDataBlobValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I8:
                case ConfigItem.ParamType.NUMBER_U8:
                case ConfigItem.ParamType.NUMBER_I16:
                case ConfigItem.ParamType.NUMBER_U16:
                case ConfigItem.ParamType.NUMBER_I32:
                case ConfigItem.ParamType.NUMBER_U32:
                    {
                        configItem = new NumberItem(itemName, numberItemOperationBoard.NumberValue, itemType);
                    }
                    break;
                case ConfigItem.ParamType.BOOL:
                    {
                        configItem = new BoolItem(itemName, boolItemOperationBoard.TrueOrFalse);
                    }
                    break;
                case ConfigItem.ParamType.STRING:
                    {
                        configItem = new StringItem(itemName, stringItemOperationBoard.StringValue);
                    }
                    break;
                case ConfigItem.ParamType.DATETIME:
                    {
                        configItem = new DateTimeItem(itemName, dateTimeOperationBoard.DateTimeValue);
                    }
                    break;

            }
            return configItem;

        }
        private void ConfigItemToControls(ConfigItem configItem)
        {
            configItemOperationPanel.Controls.Clear();
            switch (configItem.ItemType)
            {
                case ConfigItem.ParamType.BLOB:
                    {
                        configItemOperationPanel.Controls.Add(blobItemOperationBoard);
                        blobItemOperationBoard.BlobValue = ((BlobItem)configItem).BlobValue;
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I8:
                case ConfigItem.ParamType.NUMBER_U8:
                case ConfigItem.ParamType.NUMBER_I16:
                case ConfigItem.ParamType.NUMBER_U16:
                case ConfigItem.ParamType.NUMBER_I32:
                case ConfigItem.ParamType.NUMBER_U32:
                    {
                        configItemOperationPanel.Controls.Add(numberItemOperationBoard);
                        numberItemOperationBoard.NumberValue = ((NumberItem)configItem).NumValue;
                    }
                    break;
                case ConfigItem.ParamType.BOOL:
                    {
                        configItemOperationPanel.Controls.Add(boolItemOperationBoard);
                        boolItemOperationBoard.TrueOrFalse = ((BoolItem)configItem).BoolValue;
                    }
                    break;
                case ConfigItem.ParamType.STRING:
                    {
                        configItemOperationPanel.Controls.Add(stringItemOperationBoard);
                        stringItemOperationBoard.StringValue = ((StringItem)configItem).StringValue;
                    }
                    break;
                case ConfigItem.ParamType.DATETIME:
                    {
                        configItemOperationPanel.Controls.Add(dateTimeOperationBoard);
                        dateTimeOperationBoard.DateTimeValue = ((DateTimeItem)configItem).DateTimeValue;
                    }
                    break;
            }
            keyNameTextBox.Text = configItem.ItemName;
            keyTypeSelComboBox.SelectedIndex = (int)configItem.ItemType;
        }

        private void ConfigItemListViewInit(List<ConfigItem> configList)
        {
            mainListView.Items.Clear();
            mainListView.BeginUpdate();

            foreach (ConfigItem configItem in configList)
            {
                ListViewItem lvi = new ListViewItem();

                lvi.Text = configItem.ItemName;
                lvi.SubItems.Add(configItem.ItemType.ToString());
                lvi.SubItems.Add(configItem.GetFriendValueDescription());

                lvi.Tag = configItem;

                mainListView.Items.Add(lvi);
            }
            mainListView.EndUpdate();
        }


        private void 加载配置文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainOpenFileDialog.ShowDialog();
            currentConfigFilePath = mainOpenFileDialog.FileName;
            configManager.LoadConfigFromFile(currentConfigFilePath);
            ConfigItemListViewInit(configManager.ConfigList);
        }

        private void 另存配置文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainSaveFileDialog.ShowDialog();
            configManager.WriteConfigToFile(mainSaveFileDialog.FileName);
        }

        private void itemDeleteButton_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in mainListView.SelectedItems)
            {
                configManager.ConfigList.Remove((ConfigItem)lvi.Tag);
                mainListView.Items.Remove(lvi);
            }
        }


        private void itemInsertButton_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem();

            currentConfigItem = ControlsToConfigItem();

            ConfigItem cfgItem = currentConfigItem;

            lvi.Text = cfgItem.ItemName;

            lvi.Tag = cfgItem;

            lvi.SubItems.Add(cfgItem.ItemType.ToString());
            lvi.SubItems.Add(cfgItem.GetFriendValueDescription());

            if (mainListView.SelectedItems.Count>0)
            {
                configManager.ConfigList.Insert(mainListView.SelectedItems[0].Index, cfgItem);
                mainListView.Items.Insert(mainListView.SelectedItems[0].Index, lvi);
            }
            else
            {
                mainListView.Items.Add(lvi);
                configManager.ConfigList.Add(cfgItem);
            }
        }

        private void itemEditButton_Click(object sender, EventArgs e)
        {
            currentConfigItem = ControlsToConfigItem();
            configManager.ConfigList[mainListView.SelectedItems[0].Index] = currentConfigItem;
            mainListView.SelectedItems[0].Tag = currentConfigItem;
            mainListView.SelectedItems[0].SubItems[0].Text = currentConfigItem.ItemName;
            mainListView.SelectedItems[0].SubItems[1].Text =currentConfigItem.ItemType.ToString();
            mainListView.SelectedItems[0].SubItems[2].Text = (currentConfigItem.GetFriendValueDescription());
        }

        private void mainListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            currentConfigItem = (ConfigItem)(e.Item.Tag);
            ConfigItemToControls(currentConfigItem);
        }

        private void keyNameTextBox_Validated(object sender, EventArgs e)
        {
            currentConfigItem = ControlsToConfigItem();
        }

        private void keyTypeSelComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentConfigItem = ControlsToConfigItem();
            ConfigItemToControls(currentConfigItem);
        }

        private void 保存配置文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (currentConfigFilePath != null)
            {
                configManager.WriteConfigToFile(currentConfigFilePath);
            }
            else
            {
                另存配置文件ToolStripMenuItem_Click(sender, e);
            }
        }

        private void 导出CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (configManager != null)
            {
                cSytleArraySourceFileExportDialog.ShowDialog();
                configManager.GenCStyleSourceCode(cSytleArraySourceFileExportDialog.FileName);
            }
            
        }
    }
}
